package edu.umb.cs.antmanager.manager;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import static edu.umb.cs.antmanager.common.Constants.*;

public class LogBuilder {
	private static final String LOG_FILENAME = "log";
	private Document logDocument;
	private Element testResults;
	private static LogBuilder logBuilder;

	public static synchronized LogBuilder getLogBuilder() {
		if (logBuilder == null)
			logBuilder = new LogBuilder();
		return logBuilder;
	}

	/**
	 * Create a new XML document for result logging
	 */
	private LogBuilder() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			DOMImplementation impl = builder.getDOMImplementation();

			logDocument = impl.createDocument(null, "testResults", null);
			testResults = logDocument.getDocumentElement();
			saveDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Save the XML Document to log file
	 */

	private void saveDocument() {
		TransformerFactory xformFactory = TransformerFactory.newInstance();
		try {
			Transformer xmlLogTransformer = xformFactory.newTransformer();
			xmlLogTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			xmlLogTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
			Source input = new DOMSource(logDocument);
			Result xmlOutput = new StreamResult(new File(ANT_MANAGER_HOME+"\\"+LOG_FILENAME + ".xml"));
			xmlLogTransformer.transform(input, xmlOutput);

			// convert XML log to HTML log using XSLT
			Result htmlOutput = new StreamResult(new File(ANT_MANAGER_HOME+"\\"+LOG_FILENAME
					+ ".html"));
			Source styleSheet = new StreamSource(new File(ANT_MANAGER_HOME+"\\"+"buildLog.xsl"));
			Transformer htmlLogTransformer = xformFactory
					.newTransformer(styleSheet);
			htmlLogTransformer.transform(input, htmlOutput);
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Add a test result to the results log file
	 * 
	 * @param xmlStream
	 *            - the <tt>InputStream<tt> contains the test result
	 */
	public void addTestResult(InputStream xmlStream) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(xmlStream);
			Node build = doc.getDocumentElement();
			testResults.appendChild(logDocument.importNode(build, true));
			saveDocument();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
